// 使用AbortController

const controller = new AbortController();
const signal = controller.signal;

// 创建一个可以被取消的 Promise
const myPromise = new Promise((resolve, reject) => {
  const timeout = setTimeout(() => {
    resolve('Completed');
  }, 5000);

  signal.addEventListener('abort', () => {
    clearTimeout(timeout);
    reject('Promise cancelled');
  });
});

myPromise
  .then(result => console.log(result))
  .catch(error => console.log(error));

// 取消 Promise
controller.abort();

// 2、使用外部标志位来模拟取消
// let isCancelled = false;

// const myPromise = new Promise((resolve, reject) => {
//   setTimeout(() => {
//     if (isCancelled) {
//       reject('Promise cancelled');
//     } else {
//       resolve('Completed');
//     }
//   }, 5000);
// });

// // 取消逻辑
// setTimeout(() => {
//   isCancelled = true; // 设置取消标志
// }, 2000);

// myPromise
//   .then(result => console.log(result))
//   .catch(error => console.log(error));
